home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1026 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  89 lines

  1. Path: gate.net!pslfl2-51
  2. From: bhutto@gate.net (William Hutto)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: array of pointers to a tree structure ?  ( right post, the other one has a miss * )
  5. Date: 10 Jan 1996 22:40:20 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4d1f8k$ni2@news.gate.net>
  8. References: <4d06ge$9lc@hermes.fundp.ac.be>
  9. NNTP-Posting-Host: pslfl2-25.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4d06ge$9lc@hermes.fundp.ac.be>,
  13.    Francisco Melo Ledermann <fmelo@biq.fundp.ac.be> wrote:
  14. >Hi Colleagues, I have been learning C language just from one month ago 
  15. >and I don't know too much about this language. I have a problem with the 
  16. >assignment of pointers in an array of pointers with a specifical defined 
  17. >structure. For example:
  18. >
  19. >
  20. >/* BOXES STRUCTURE FOR THE CONSTRUCTION OF A TREE */
  21. >
  22. >struct boxes {
  23. >              int number;
  24. >              struct boxes *pointer_1;
  25. >              struct boxes *pointer_2;
  26. >              struct boxes *pointer_3;
  27. >             }
  28.                ^
  29. Semicolon goes here.
  30.  
  31. >
  32. >/* ARRAY OF POINTERS TO BOXES STRUCTURE FOR MANAGE */
  33. >/* SOME BRANCHES OF THE TREE                       */
  34. >
  35. >struct boxes *array_pointers [15];
  36.  
  37. Now this makes a big difference from your first post (which I answered but 
  38. didn't send). You've defined and array of 15 pointers of type _struct boxes_ 
  39. which point to NULL if statically defined or to *God knows where* if they're 
  40. automatic.
  41.  
  42. >
  43. >
  44. >Supose that I have the array of pointers already defined, with every 
  45. >pointer pointing to a boxes structure and every boxes structure contains 
  46. >only the integer number defined. In this moment I would like to define 
  47. >the three pointers in every box in the way of these pointers point to 
  48. >another element in the array. I have tried statements like this with out 
  49. >success:
  50. >
  51. >
  52. >(*(array_pointers + 1)).pointer_1 = (array_pointers + 0);
  53. >
  54. >or
  55. >
  56. >(array_pointers[1]).pointer_1 = &array_pointers[0];
  57. >                                 
  58.  
  59. You're trying to access members of a structure that does not physically exist. 
  60. All pointers should be properly initialized, as per type, before using them. 
  61. In this case, you could use malloc() (#include <stdlib.h>). Also, you would 
  62. use the indirect member selector (->) to access members of the structures.
  63.  
  64. func()
  65. {
  66. int i;
  67. struct boxes *array_pointers[15];
  68.  
  69.     for(i=0;i<15;i++) {
  70.         if((array_pointers[i]=malloc(sizeof(struct boxes)))==NULL) {
  71.             /*ERROR: Insufficient memory*/
  72.         }
  73.     }
  74.     array_pointers[1]->pointer_1 = array_pointers[0];
  75.     
  76.     array_pointers[1]->pointer_1->number=1234;
  77.     
  78.     printf("%d\n",array_pointers[0]->number);  /*prints 1234*/
  79. }
  80.  
  81. Read the comp.lang.c FAQ which contains several chapters on information you 
  82. need here.
  83.  
  84. ftp://rtfm.mit.edu/pub/usenet-by-group/comp.lang.c/C-FAQ-list
  85.  
  86. Bill
  87.  
  88. "Whatcha got on?...Your mind?"
  89.